<template>
  <div class="score-dialog" @click.stop>
    <div class="title">院校分数区间范围（可拖动）</div>
    <div class="popup-score">
      <div class="reset-box" @click="resetScoreRange">
        <img class="reset-btn" :src="resetBtn" alt="" />
        <div class="content">恢复推荐区间</div>
      </div>
      <div class="slider-container">
        <div class="left-number">{{ minScore }}</div>
        <van-slider
          v-model="scoreRange"
          :min="minScore"
          :max="maxScore"
          range
          bar-height="8px"
          active-color="$primary-color"
          @change="onChange"
        >
          <template #left-button>
            <div class="left-button">{{ scoreRange[0] }}</div>
          </template>
          <template #right-button>
            <div class="right-button">{{ scoreRange[1] }}</div>
          </template>
        </van-slider>
        <div class="right-number">{{ maxScore }}</div>
      </div>
    </div>
    <div class="bottom-btn">
      <div class="right-btn" @click="confirm">确 定</div>
    </div>
  </div>
</template>

<script setup>
import { ref, watchEffect } from 'vue';
import resetBtn from '@/assets/images/reset.png';

const emit = defineEmits(['confirm']);

const props = defineProps({
  gradeInfo: {
    type: Object,
    default: () => ({}),
  },
});

const scoreCache = ref([0, 0]);
const scoreRange = ref([0, 0]);
const minScore = ref(0);
const maxScore = ref(0);
watchEffect(() => {
  minScore.value = Math.floor(
    Number(props.gradeInfo.score) - 150 >= 0
      ? Number(props.gradeInfo.score) - 150
      : 0
  );
  maxScore.value = Math.floor(
    Number(props.gradeInfo.score) + 100 <= 750
      ? Number(props.gradeInfo.score) + 100
      : 750
  );
  const range1 = Math.floor(
    Number(props.gradeInfo.score) - 70 >= 0
      ? Number(props.gradeInfo.score) - 70
      : 0
  );
  const range2 = Math.floor(
    Number(props.gradeInfo.score) + 30 >= 750
      ? 750
      : Number(props.gradeInfo.score) + 30
  );
  scoreRange.value = [range1, range2];
  scoreCache.value = [range1, range2];
});

const resetScoreRange = () => {
  scoreRange.value = scoreCache.value.map(Math.floor);
};

const onChange = (value) => {
  scoreRange.value = value.map(Math.floor);
};

const confirm = () => {
  emit('confirm', scoreRange.value);
};
</script>

<style scoped lang="scss">
.score-dialog {
  position: absolute;
  top: 60px;
  left: 0;
  width: 580px;
  z-index: 99;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0px 2px 28px 6px rgba(90, 122, 162, 0.3);
  overflow: hidden;
}
.title {
  padding: 32px 0 24px 32px;
  display: flex;
  align-items: flex-end;
  font-family: PingFang-SC, PingFang-SC;
  font-weight: bold;
  font-size: 28px;
  color: #222222;
  line-height: 22px;
  text-align: left;
  font-style: normal;
  span {
    font-size: 22px;
    color: #999999;
  }
}
.popup-score {
  height: 160px;
  padding: 0 32px 110px 32px;
  .reset-box {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-top: 20px;
    margin-bottom: 42px;
    width: fit-content;
    .reset-btn {
      width: 32px;
      height: 32px;
      margin-right: 16px;
    }
    .content {
      height: 22px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 26px;
      color: $primary-color;
      line-height: 22px;
    }
  }
  .left-button,
  .right-button {
    color: #fff;
    padding: 6px 14px;
    border-radius: 100px;
    border: 4px solid #fff;
    background-color: $primary-color;
    font-size: 26px;
  }
  .slider-container {
    display: flex;
    align-items: center;
    .left-number,
    .right-number {
      font-size: 26px;
      color: #888;
    }
  }
}
.bottom-btn {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 24px;
  background: #ffffff;
  box-shadow: 0px -2px 4px 0px rgba(188, 188, 188, 0.5);
  box-sizing: border-box;
  display: flex;
  flex-direction: row;
  justify-content: center;
  gap: 24px;
  z-index: 2;

  .right-btn {
    flex-shrink: 0;
    width: 100%;
    height: 62px;
    background: #f8f8f8;
    border-radius: 6px;
    font-family: PingFang-SC, PingFang-SC;
    font-weight: bold;
    font-size: 24px;
    color: #999999;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
  }
  .right-btn {
    color: #ffffff;
    background: $primary-color;
  }
}
</style>
